from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64
import os,sys
rsa_key = RSA.generate(1024)
BASE_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
encrypt_path = os.path.join(BASE_DIR, 'app/certificate/public.pem')
decrypt_path = os.path.join(BASE_DIR, 'app/certificate/private.pem')
def encrypt_data(data):
    with open(encrypt_path,'rb') as f:
        public=f.read()
    rsa_pkey = RSA.import_key(public)
    rsa_ = PKCS1_v1_5.new(key=rsa_pkey)
    result = rsa_.encrypt(data.encode())
    b64_result = base64.b64encode(result).decode()
    return b64_result

def decrypt_data(data):
    with open(decrypt_path,'rb') as f:
        private=f.read()
    rsa_pkey = RSA.import_key(private)
    rsa_ = PKCS1_v1_5.new(key=rsa_pkey)
    result = rsa_.decrypt(base64.b64decode(data),None).decode()
    return result





